﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using PublishNews.Entities;
using PublishNews.Models;

namespace PublishNews.Controllers
{
    [Authorize]
    public class UserController : Controller
    {
        //
        // GET: /LocationApi/
        public UserManager<User> UserManager { get; private set; }

        public UserController()
        {
            UserManager = new UserManager<User>(new UserStore<User>(new PublishNewsContext()));
        }
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View(new UserModel());
        }

        [HttpPost]
        public ActionResult Create(UserModel model)
        {
            if (ModelState.IsValid)
            {
                User user = new User
                {
                    UserName = model.UserName,
                    Email = model.UserName,
                    PhoneNumber = model.PhoneNumber,
                    FullName = model.FullName,
                    IsAdministrator = model.IsAdministrator,
                    HideEmail = model.HideEmail,
                    RejectNews = model.RejectNews
                };

                IdentityResult result = UserManager.Create(user, model.Password);

                if (!result.Succeeded)
                {
                    if (result.Errors != null)
                    {
                        foreach (string error in result.Errors)
                        {
                            ModelState.AddModelError("", error);
                        }
                    }
                }
                model.Succeeded = result.Succeeded;
            }
            return View(model);
        }

        public ActionResult Edit(string id)
        {
            var oldUser = UserManager.FindById(id);
            var user = Mapper.Map<EditUserModel>(oldUser);

            return View(user);
        }

        [HttpPost]
        public ActionResult Edit(EditUserModel model)
        {
            if (ModelState.IsValid)
            {
                var oldUser = UserManager.FindById(model.Id);

                oldUser.FullName = model.FullName;
                oldUser.PhoneNumber = model.PhoneNumber;
                oldUser.Fax = model.Fax;
                oldUser.IsAdministrator = model.IsAdministrator;
                oldUser.HideEmail = model.HideEmail;
                oldUser.RejectNews = model.RejectNews;

                IdentityResult result = UserManager.Update(oldUser);

                if (!result.Succeeded)
                {
                    if (result.Errors != null)
                    {
                        foreach (string error in result.Errors)
                        {
                            ModelState.AddModelError("", error);
                        }
                    }
                }
                model.Succeeded = result.Succeeded;
            }
            return View(model);
        }

        public ActionResult ChangePassword(string id)
        {
            var model = new ChangePasswordModel()
                            {
                                UserId = id
                            };
            return View(model);
        }

        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            IdentityResult result = UserManager.ChangePassword(model.UserId, model.CurrentPassword, model.NewPassword);
            model.Succeeded = result.Succeeded;

            return View(model);
        }

        public JsonResult Get()
        {
            var result = UserManager.Users.ToList();
            var users = Mapper.Map<List<UserModel>>(result);
            return Json(users, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Delete(UserModel model)
        {
            var user = UserManager.FindById(model.Id);
            IdentityResult result = new IdentityResult();
            if (user != null)
            {
                result = UserManager.Delete(user);
            }
            return Json(result.Succeeded, JsonRequestBehavior.AllowGet);
        }
    }
}
